//问题模块
var get_question_module = function(){
    var models='';
    $.ajax({
        url: WebSite.AllQuestionPage.allQuestion_template,
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                var myTemplate = Handlebars.compile($("#allQuestion_questiontemplate").html());
                $("#questiontemplate").html(myTemplate(data));

                $(".tag-module-all").toggleClass("tag-selected");

                $(".tag-module-all").click(function(){
                    $(".tag-module").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    searchContext();
                });

                $(".tag-module").click(function(){

                    $(".tag-module-all").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    if ($(this).hasClass("tag-module-all")){
                        return;
                    }
                    var modules_arr  = [];

                    $(".tag-module.tag-selected").each(function(index, ele) {
                        modules_arr.push($(ele).attr("data-module"));
                    });

                    $("#model").val(modules_arr);

                    searchContext();
                });

            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};

//问题类型模块
var get_type_module = function(){
   // alert(WebSite.AllQuestionPage.allQuestion_versiontemplate);
    $.ajax({
        url: WebSite.AllQuestionPage.allquestion_type,
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                var myTemplate = Handlebars.compile($("#allQuestion_typetemplate").html());
                $("#typetemplate").html(myTemplate(data));

                $(".tag-type-all").toggleClass("tag-selected");

                $(".tag-type-all").click(function(){
                    $(".tag-type").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    searchContext();
                });

                $(".tag-type").click(function(){

                    $(".tag-type-all").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    if ($(this).hasClass("tag-type-all")){
                        return;
                    }
                    var types_arr  = [];

                    $(".tag-type.tag-selected").each(function(index, ele) {
                        types_arr.push($(ele).attr("data-type"));
                    });

                    $("#type").val(types_arr);
                    //console.log(modules_arr);
                    searchContext();
                });

            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};

//版本模块
var get_version_module = function(){
    $.ajax({
        url: WebSite.AllQuestionPage.allQuestion_versiontemplate,
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                var myTemplate_version = Handlebars.compile($("#allQuestion_versiontemplate").html());
                $("#versiontemplate").html(myTemplate_version(data));

                $(".tag-version-all").toggleClass("tag-selected");

                $(".tag-version-all").click(function(){
                    $(".tag-version").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    searchContext();
                });

                $(".tag-version").click(function(){
                    $(".tag-version-all").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    if ($(this).hasClass("tag-version-all")){
                        return;
                    };
                    var versions_arr = [];
                    $(".tag-version.tag-selected").each(function(index, ele) {
                        versions_arr.push($(ele).attr("data-version"));
                    });
                    $("#vision").val(versions_arr);
                    //console.log(versions_arr);
                    searchContext();
                });

            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};


//点击全部时发送请求的方法
var searchContext_all = function(pageno){
    if(pageno==null){
        pageno=1;
    }
    var versions_arr = [];
    var modules_arr  = [];
    var types_arr     = [];
    $(".tag-module.tag-selected").each(function(index, ele) {
        modules_arr.push($(ele).attr("data-module"));
    });
    $(".tag-type.tag-selected").each(function(index, ele) {
        types_arr.push($(ele).attr("data-type"));
    });
    $(".tag-version.tag-selected").each(function(index, ele) {
        versions_arr.push($(ele).attr("data-version"));
    });

    var extParams = $.param({
        "vision": versions_arr,
        "type"  : types_arr,
        "model" : modules_arr
    }, true);
    //alert(WebSite.AllQuestionPage.search + "&" + extParams);
    $.ajax({
        url: WebSite.SearchResultPage.searchResult_list + "&" + extParams,
        type: "post",
        data: {
            "keyword": $("#kw").val(),
            "pageno":  pageno,
            "methodname":"searchContext_all"
        },
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                for (var i = 0, len = data.length; i < len; i++) {
                    var record = data[i];
                    record.ALLQ_link = searchDetailPath(record);
                }
                var pagemess=result.pagemess;
                $('#pagemes').html(pagemess);
                var myTemplate = Handlebars.compile($("#template").html());
                $("#contain-all-left").html(myTemplate(data));
            }
        },
        error: function () {
            alert("没有获得22数据");
        }
    });
};

//点击版本，问题类型，版本时调的方法（不含点击各部分的全部）
var searchContext = function(pageno){
    if(pageno==null){
        pageno=1;
    }

    if ($(".tag-module.tag-selected").size() < 1) {
        $(".tag-module-all").toggleClass("tag-selected");
    }

    if ($(".tag-type.tag-selected").size() < 1) {
        $(".tag-type-all").toggleClass("tag-selected");
    }

    if ($(".tag-version.tag-selected").size() < 1) {
        $(".tag-version-all").toggleClass("tag-selected");
    }

    if ($(".tag-module.tag-module-all.tag-selected").size() > 0) {
        searchContext_all();
        return;
    }

    var versions_arr = [];
    var modules_arr  = [];
    var types_arr     = [];


    $(".tag-module.tag-selected").each(function(index, ele) {
        modules_arr.push($(ele).attr("data-module"));
    });
    $(".tag-type.tag-selected").each(function(index, ele) {
        types_arr.push($(ele).attr("data-type"));
    });
    $(".tag-version.tag-selected").each(function(index, ele) {
        versions_arr.push($(ele).attr("data-version"));
    });


    var extParams = $.param({
        "vision": versions_arr,
        "type"  : types_arr,
        "model" : modules_arr
    }, true);

    /*console.log(extParams);*/

    //alert(WebSite.AllQuestionPage.search + "&" + extParams);
    $.ajax({
        url: WebSite.SearchResultPage.searchResult_list + "&" + extParams,
        type: "post",
        data: {
            "keyword": $("#kw").val(),
            "pageno":  pageno,
            "methodname":"searchContext"

        },
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                for (var i = 0, len = data.length; i < len; i++) {
                    var record = data[i];
                    record.ALLQ_link = searchDetailPath(record);
                }
                var pagemess=result.pagemess;
                $('#pagemes').html(pagemess);
                var myTemplate = Handlebars.compile($("#template").html());
                $("#contain-all-left").html(myTemplate(data));
            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
}

var registerHelper_status = function () {
}


Handlebars.registerHelper("compare",function(v1,options){
    if(v1 == 3){
        return options.fn(this);
    }
    else{
        return options.inverse(this);
    }
});